package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootFormation;
import org.footballmanager.pandafootball.system.form.impl.FootFormationSearchForm;
import org.footballmanager.pandafootball.system.service.FootFormationService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class FootFormationServiceImpl extends ServiceImpl<FootFormation> implements FootFormationService {
    /**
     * 查询
     *
     * @param entityname              实体类名
     * @param map                     参数
     * @param footFormationSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootFormationSearchForm footFormationSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footFormationSearchForm);
        Page<FootFormation> page = PageHelper.startPage(footFormationSearchForm.getPageIndex(), footFormationSearchForm.getPageSize());
        List<FootFormation> list = this.findall(footFormationSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footFormationSearchForm 条件
     * @param request                 请求
     */
    @Override
    public void deleteAll(FootFormationSearchForm footFormationSearchForm, HttpServletRequest request) {
        if (footFormationSearchForm.getSelectIds() != null) {
            String[] result = footFormationSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 获取默认阵型
     *
     * @return 阵型主键
     */
    @Override
    public Long getDefaultFormation() {
        Example example = new Example(FootFormation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example).getId();
    }
}
